<template>
    <div class="createPost-container">
        <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">
            <div class="createPost-main-container">
                <el-form-item prop="corpid" style="margin-bottom: 40px;" label-width="160px" label="企业ID:">
                    <el-input v-model="postForm.corpid" :rows="1" class="article-textarea" autosize placeholder="请输入企业ID" />
                </el-form-item>
                <el-form-item prop="corpsecret" style="margin-bottom: 40px;" label-width="160px" label="通讯录同步secret:">
                    <el-input v-model="postForm.corpsecret" :rows="1" type="password" class="article-textarea" autosize placeholder="请输入通讯录同步secret" />
                </el-form-item>
                <el-button v-loading="loading" style="margin-left: 10px;" type="primary" @click="submitForm">
                    保存
                </el-button>
            </div>
        </el-form>
    </div>
</template>

<script>
import { fetch, update } from '@/platform/api/conf/qiwei'

const defaultForm = {
    corpid: '',
    corpsecret: ''
}

export default {
    name: 'MiniappDetail',
    data() {
        const validateRequire = (rule, value, callback) => {
            if (value === '' || value === null || value === undefined) {
                this.$message({
                    message: rule.message,
                    type: 'error'
                })
                callback(new Error())
            } else {
                callback()
            }
        }
        return {
            postForm: Object.assign({}, defaultForm),
            loading: false,
            userListOptions: [],
            rules: {
                corpid: [{ validator: validateRequire, message: '企业ID为必填字段', trigger: 'blur' }],
                corpsecret: [{ validator: validateRequire, message: '通讯录同步secret为必填字段', trigger: 'blur' }]
            },
            tempRoute: {},
            dialogFormGps: false
        }
    },
    created() {
        this.fetchData()
    },
    methods: {
        fetchData() {
            fetch().then(response => {
                this.postForm.corpid = response.data.corpid
                this.postForm.corpsecret = response.data.corpsecret
                // set page title
            }).catch(err => {
                console.log(err)
            })
        },
        submitForm() {
            this.$refs.postForm.validate(valid => {
                if (valid) {
                    this.loading = true
                    const data = this.postForm
                    update(data).then(response => {
                        this.$notify({
                            title: '成功',
                            message: '修改成功',
                            type: 'success',
                            duration: 2000
                        })
                        // this.postForm.status = 'published'
                        this.loading = false
                        // this.$router.push('/store/index')
                    }).catch(err => {
                        console.log(err)
                        this.loading = false
                    })
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        }
    }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
    position: relative;

    .createPost-main-container {
        padding: 40px 45px 20px 50px;

        .postInfo-container {
            position: relative;
            @include clearfix;
            margin-bottom: 10px;

            .postInfo-container-item {
                float: left;
            }
        }
    }

    .word-counter {
        width: 40px;
        position: absolute;
        right: 10px;
        top: 0px;
    }
}

.article-textarea ::v-deep {
    textarea {
        padding-right: 40px;
        resize: none;
        border: none;
        border-radius: 0px;
        border-bottom: 1px solid #bfcbd9;
    }
}
</style>
